package com.gather.hashsets;

import com.gather.orm.User;

import java.util.HashSet;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

// 练习题目
public class Example04 {
    public static void main(String[] args) {
        HashSet<User> users = new HashSet<>();
        // 事先添加用户
        User user1 = new User("张正","123456","123456","87@qq.com","123","1996");
        users.add(user1);

        System.out.println("练习题目");
        // 用户控制台输入
        Scanner scanner = new Scanner(System.in);
        System.out.printf("请输入用户名：");
        String name = scanner.next();

        System.out.printf("请输入密码：");
        String pwd = scanner.next();

        System.out.printf("请重复密码：");
        String password = scanner.next();
        // 校验
        if (!pwd.equals(password)) {
            System.out.println("两次密码输入不一致！");
            return;
        }
        System.out.printf("出生日期：");
        String birthday = scanner.next();
        // 正则表达式
        /**
         * 正则表达式步骤
         * 1.正则
         * 2.使用 Pattern.compile 构建正则
         * 3.匹配 matcher
         * 4.matches() 返回匹配结果
         */
        String checkBirthday = "^(\\d{2}|\\d{4})[-/](0?[1-9]|1[1,2])[-/](0?[1-9]|[12]\\d|3[01])$";
        Pattern regexBirthday = Pattern.compile(checkBirthday);
        Matcher matcherBirthday = regexBirthday.matcher(birthday);
        boolean matchesBirthday = matcherBirthday.matches();
        if (!matchesBirthday) {
            System.out.println("出生日期格式不对！");
            return;
        }

        System.out.printf("手机号码：");
        String phone = scanner.next();
        // 验证手机号码
        String checkPhone = "^(1[3,5,7,8])\\d{9}$";
        Pattern regexPhone = Pattern.compile(checkPhone);
        Matcher matcherPhone = regexPhone.matcher(phone);
        boolean matches = matcherPhone.matches();
        if (!matches || phone.length() != 11) {
            System.out.println("手机号码格式不对！");
            return;
        }

        // 邮箱正则表达式
        String checkEmail = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        System.out.printf("电子邮箱：");
        String email = scanner.next();
        // 检测邮箱
        Pattern regex = Pattern.compile(checkEmail);
        Matcher matcher = regex.matcher(email);
        boolean isMatched = matcher.matches();
        if (!isMatched) {
            System.out.println("邮箱格式不正确！");
            return;
        }

        User user = new User(name, pwd, password, email, phone, birthday);
        boolean r = users.add(user);
        System.out.printf("注册结果："+ (r ? "注册成功" : "注册失败,用户重复"));
        scanner.close();
    }
}
